#include "user_bt_call.h"
#include "FreeRTOS.h"
#include "user_bt_common.h"
#include "app_lvgl.h"
#include "task.h"
#include "timers.h"
#include "app_bt.h"
#include "user_bt.h"

#define BT_CALL_LOG    printf

#define BT_LOG_ENABLE   1
#if BT_LOG_ENABLE
#define BT_LOG  printf
#else
#define BT_LOG
#endif

call_info_t  bt_call;
//bool call_flag = 0;
uint8_t call_incoming = 0;   //呼入呼出状态
bool call_complete = 0;     //接通状态

xTimerHandle user_bt_call_timer;
//0：呼出，1：接通，2：静音，3：通话结束
//4：拨出失败，5：来电显示
uint8_t call_mode;

void    user_set_call_mode(uint8_t mode)
{
    call_mode = mode;
}

uint8_t    user_get_call_mode(void)
{
    return call_mode;
}

void    bt_hf_call_outgoing(uint8_t *number, uint8_t len)
{
    BtStatus status;
    status = bt_dial_number(user_bt_env.last_active_index,&number[1],number[0]);    

    BT_CALL_LOG("%s %d \r\n", __func__,status);
}



void    bt_hf_call_hangup(void)
{
    BtStatus status;
    status = bt_hang_up(user_bt_env.last_active_index);

    BT_CALL_LOG("%s %d \r\n", __func__,status);
}  


void    bt_hf_call_answer(void)
{
    BtStatus status;
    status = bt_answer_call(user_bt_env.last_active_index);

    BT_CALL_LOG("%s %d \r\n", __func__,status);
}  

void    user_bt_hf_mic_mute(bool mute)
{
    if(mute)
    {
        //__CODEC_SET_ADC_VOLUME(0);
    }else {
        //__CODEC_SET_ADC_VOLUME(0x8000);
    }
}


void    user_bt_hfcall_handle(uint8_t hf_call)
{
	if(hf_call == 0)            //挂断
	{
        if(call_complete)
        {
            call_complete = 0;
            gui_task_msg_send(PHONE_CALL_REJECT_EVT,NULL,0,NULL,0,NULL);
        }
	}
    else if(hf_call == 1)       //接通
    {
        user_set_call_mode(HF_ENUM_CALL_CONNECTING);
        call_incoming = 0;      
        call_complete = 1;      //接通状态
        gui_task_msg_send(PHONE_CALL_OK_EVT,NULL,0,NULL,0,NULL);
    }

}

void    user_bt_callsetup_handle(uint8_t hf_callsetup)
{
	if(hf_callsetup == 0)
	{
        if(call_incoming == 1 || call_incoming == 2) //         
        {
            call_incoming = 0;        
            gui_task_msg_send(PHONE_CALL_REJECT_EVT,NULL,0,NULL,0,NULL);           
        }
	}
    else if(hf_callsetup == 1)    /*      incoming call    */       
    {
      

        call_incoming = 1;        //来电状态置1
        user_set_call_mode(5);      
    
    }
    else if(hf_callsetup == 2)      /*      outgoing call    */           
    {
        

        call_incoming = 2;          //呼出状态
        user_set_call_mode(0);  
    }
}

void    user_bt_set_calling_num(const char *num, uint8_t len)
{

    bt_call.number_len = len;
    memset(&bt_call.call_namber[0], 0,      sizeof(bt_call.call_namber));
    memcpy(&bt_call.call_namber[0], num,    len);

    if(call_incoming)
    {
        app_task_wakeup_gui_task();
        gui_task_msg_send(PHONE_CALL_IN_EVT,NULL,0,NULL,0,NULL);
    }
    
}



void    user_bt_get_calling_info(call_info_t * call)
{
    #if 1
        call->number_len = bt_call.number_len;
        memset(call->call_namber,0,32);
        memcpy(call->call_namber,bt_call.call_namber,call->number_len);

	    //call->name_len = call_context[call->number_len] + 1;
        //memset(call->call_name,0,128);
        //memcpy(call->call_name, call_context+call->number_len+1, call->name_len);

    #else
    if(call_context[0] > 0)
    {
        call->number_len = call_context[0];
        memset(call->call_namber,0,32);
        memcpy(call->call_namber,call_context+1,call->number_len);

	    call->name_len = call_context[call->number_len] + 1;
        memset(call->call_name,0,128);
        memcpy(call->call_name, call_context+call->number_len+1, call->name_len);
    }
	#endif
}

static void user_bt_timer_call_handler( TimerHandle_t xTimer)
{
    BT_LOG("%s \r\n",__func__);
}

void    user_bt_timer_call_init(void)
{
    user_bt_call_timer = xTimerCreate("user_bt_call_timer",  100 , pdFALSE, NULL, user_bt_timer_call_handler);
    //xTimerStart(user_bt_call_timer,0);
}


void    user_bt_hf_set_volume(uint8_t lev)
{

    if(lev == 0){

        user_bt_set_local_volume(BT_VOL_HFP, 1);

    }else{ 

        user_bt_set_local_volume(BT_VOL_HFP, lev);
    }    
    BT_LOG("%s %d \r\n", __func__, lev);

}






